<?php

namespace App\Http\Controllers\Api;

use App\Repositories\Contracts\PolicyCommonUploadInterface;
use App\Repositories\Contracts\PolicyUploadInterface;
use Illuminate\Http\Request;
use MakeRep\Controllers\BaseController;

class TcNotifyController extends BaseController

{
    private $commonInterface;

    public function __construct(PolicyUploadInterface $interface, PolicyCommonUploadInterface $commonInterface)
    {
        parent::__construct($interface);
        $this->commonInterface = $commonInterface;
    }


    /**
     * 同程特政新增、更新、删除政策回调
     * @param Request $request
     * @return string
     */
    public function notify(Request $request)
    {

        $param = $request->all();
        logger($param);
        //logger(json_decode(gzdecode(base64_decode($param['tradeResult'])), true));
        if (!empty($param) && !empty($param['responseStatus'])) {
            $responseStatus = $param['responseStatus'];
            $where = ['ext' => $responseStatus['requestGuid']];
            //logger($where);
            $policy = $this->interface->index($where);

            if ($policy) {
                $tradeResult = json_decode(gzdecode(base64_decode($param['tradeResult'])), true);
                if ($tradeResult) {
                    $summary = $tradeResult['summary'];
                    $interfaceName = $responseStatus['interfaceName'];
                    if ($summary['successCount'] == 1) {
                        $successList = $tradeResult['successList'][0];
                        $cabin = $policy['cabin'];
                        if ($interfaceName == 'specialPolicyAdd') {

                            $policyId = $successList['policyId'];
                            $cabin = $this->getCabin($policyId, $cabin);

                            $data = [
                                'policy_id' => $policyId,
                                'upload_status' => 2,
                                'cabin' => $cabin
                            ];
                        } else if ($interfaceName == 'specialPolicyUpdate') {

                            $policyId = $successList;
                            $cabin = $this->getCabin($policyId, $cabin);

                            $data = [
                                'policy_id' => $policyId,
                                'upload_status' => 2,
                                'cabin' => $cabin
                            ];
                        } else {
                            $data = [
                                'status' => -1,
                            ];
                        }
                    } else {
                        $errMsg = $tradeResult['failedList'][0]['errMsg'];
                        if ($interfaceName == 'specialPolicyAdd') {
                            $data = [
                                'upload_status' => 3,
                                'upload_msg' => $errMsg,
                            ];
                        } else if ($interfaceName == 'specialPolicyUpdate') {
                            $data = [
                                'upload_status' => 4,
                                'upload_msg' => $errMsg,
                            ];
                        } else {
                            $data = [
                                'upload_msg' => $errMsg,
                            ];
                            if ($errMsg == '删除成功，无数据') {

                                $data['deleted_at'] = date('Y-m-d H:i:s');
                            }
                        }
                    }

                    if (!empty($data['status']) && $data['status'] == -1) {
                        $this->interface->delete($where);
                    } else {
                        $this->interface->update($where, $data);
                    }
                }
            }
        }
        return "SUCCESS";
    }


    /**
     * 同程普政新增、更新、删除政策回调
     * @param Request $request
     * @return string
     */
    public function commonNotify(Request $request)
    {

        $param = $request->all();
        logger($param);
        logger(json_decode(gzdecode(base64_decode($param['tradeResult'])), true));
        if (!empty($param) && !empty($param['responseStatus'])) {
            $responseStatus = $param['responseStatus'];
            $where = ['ext' => $responseStatus['requestGuid']];
            $policy = $this->commonInterface->index($where);
            logger($policy);
            if ($policy) {
                $tradeResult = json_decode(gzdecode(base64_decode($param['tradeResult'])), true);
                if ($tradeResult) {
                    $summary = $tradeResult['summary'];
                    $interfaceName = $responseStatus['interfaceName'];
                    if ($summary['successCount'] == 1) {
                        $successList = $tradeResult['successList'][0];
                        $cabin = $policy['cabin'];
                        if ($interfaceName == 'commonPolicyAdd') {

                            $policyId = $successList['policyId'];
                            $cabin = $this->getCabin($policyId, $cabin);

                            $data = [
                                'policy_id' => $policyId,
                                'upload_status' => 2,
                                'cabin' => $cabin
                            ];
                        } else if ($interfaceName == 'commonPolicyUpdate') {

                            $policyId = $successList;
                            $cabin = $this->getCabin($policyId, $cabin);

                            $data = [
                                'policy_id' => $policyId,
                                'upload_status' => 2,
                                'cabin' => $cabin
                            ];
                        } else {
                            $data = [
                                'status' => -1,
                            ];
                        }
                    } else {
                        $errMsg = $tradeResult['failedList'][0]['errMsg'];
                        if ($interfaceName == 'commonPolicyAdd') {
                            $data = [
                                'upload_status' => 3,
                                'upload_msg' => $errMsg,
                            ];
                        } else if ($interfaceName == 'commonPolicyUpdate') {
                            $data = [
                                'upload_status' => 4,
                                'upload_msg' => $errMsg,
                            ];
                        } else {
                            $data = [
                                'upload_msg' => $errMsg,
                            ];
                            if ($errMsg == '删除成功，无数据') {

                                $data['deleted_at'] = date('Y-m-d H:i:s');
                            }
                        }
                    }

                    if (!empty($data['status']) && $data['status'] == -1) {
                        $this->commonInterface->delete($where);
                    } else {
                        $this->commonInterface->update($where, $data);
                    }
                }
            }
        }
        return "SUCCESS";
    }

    /**
     * @param $policyId
     * @param $cabin
     */
    public function getCabin($policyId, $cabin): string
    {
        $tcPolicyInterface = resolve('App\Repositories\Ota\Tc\Policy\Contracts\TcPolicyInterface');
        $tcPolicyInfo = $tcPolicyInterface->getPolicyInfoById($policyId);
        if ($tcPolicyInfo) {
            if ($tcPolicyInfo['data']) {
                $policyRes = $tcPolicyInfo['data'][0];
                if ($policyRes) {
                    return implode(',', $policyRes['cabinCodeList']);
                }
            }
        }
        return $cabin;
    }

}